/**
 * Created by 周颖 on 2017/7/26.
 *
 * axios post请求默认提交的是JSON,如果需要提交 application/x-www-form-urlencoded =>
 * 1、引入 import queryString from 'querystring';
 * 2、参数 queryString.stringify(para)
 * [参考](https://github.com/mzabriskie/axios#using-applicationx-www-form-urlencoded-format)
 *
 *
 * [接口地址](http://59.107.26.181:8086/swagger-ui.html)
 */

import axios from 'axios';
import queryString from 'querystring';

let api = {
    postUserList: '/api-hks/v1/mgr/SysUserCtrl/getSysUserList', // 用户列表=> user/userList
    getAssignRoles: '/api-web/v1/api/user/assignRoles', // 获取分配角色权限=> Nav
    getMenuList: '/api-web/v1/api/auth/menu/role', // 获取左边菜单栏=> Nav
    postUploadImg: '/api-web/v1/api/upload', // 上传图片
    getIssuerId: '/api-hks/v1/app/ProductInfoApi/getIssuerId', //查询发行方
    postLog: '/api-web/v1/api/backend/operationLogging', //操作日志
    getMenuListLv1: '/api-web/v1/api/auth/menu/lv1', // 获取一级菜单栏
    postSortMenuList: '/api-web/v1/api/role/assignMenus/displayOrder', // 菜单栏排序
}

const throwIfMissing = (para) => {
    throw new Error(`Missing param => ${para}`);
}

const getMenuListLv1 = () => {
    return axios.get(api.getMenuListLv1)
}

const postSortMenuList = (parm) => {
    // console.log(parm)
    return axios.post(api.postSortMenuList, parm)
}

const postUserList = (obj, pageSize) => {
    let parm = {
        recommendCode: obj.recommendCode,
        beginDate: obj.beginDate,
        endDate: obj.endDate,
        tieCard: obj.tieCard,
        isLeave: obj.isLeave,
        isInside: obj.isInside,
        name: obj.name,
        actUserId: obj.actUserId,
        curPage: obj.curPage,
        pageSize: pageSize || obj.pageSize
    }
    return axios.post(api.postUserList, parm)
}

const getIssuerId = () => { return axios.get(api.getIssuerId) }


const getAssignRoles = (userId) => {
    let parm = {
        userId: userId
    }
    return axios.get(api.getAssignRoles, { params: parm })
}

const getMenuList = (roleId) => {
    let parm = {
        roleId: roleId
    }
    return axios.get(api.getMenuList, { params: parm })
}

const postUploadImg = (parm) => {
    // console.log(parm)
    return axios.post(api.postUploadImg, parm)
}

/**
 *在某些操作执行后，后台需要记录操作人操作事项等信息
 * @param {string}  event            操作事项（按钮）的唯一unique_id
 * @param {number}  [operateItemId]    操作对象的id
 * @param {string}  [operateItemName]  操作对象的名字
 * @param {object}  [eventRequestJson]  此次操作具体的内容
 */
let CIP;
try {
    CIP = returnCitySN['cip'];
} catch (e) {
    console.error(e)
}
const postLog = (event = throwIfMissing('event'), operateItemId, operateItemName, eventRequestJson) => {
    let parm = {
        event: event,
        eventRequestJson: eventRequestJson ? JSON.stringify(eventRequestJson) : null,
        operateItemId: operateItemId,
        operateItemName: operateItemName,
        operatorId: sessionStorage.getItem('HKid'),
        terminalIP: CIP
    }
    axios.post(api.postLog, parm).then((resp) => {
        console.log('日志发送成功 => ' + event)
            // console.log('日志发送成功 => ' + event, parm, resp.data)
    }).catch((err) => {
        console.error('日志发送失败 => ' + event, err)
    })
}

export {
    api,
    postUserList,
    getAssignRoles,
    getMenuList,
    getMenuListLv1,
    postSortMenuList,
    getIssuerId,
    postUploadImg,
    postLog,
}